<%@ page import="com.ld.manageBook.service.BorrowService" %>
<%@ page import="com.ld.manageBook.service.impl.BorrowImpl" %>
<%@ page import="com.ld.manageBook.bean.User" %>
<%@ page import="com.ld.manageBook.service.ReturnService" %>
<%@ page import="com.ld.manageBook.service.impl.ReturnServiceImpl" %>
<%@ page import="com.ld.manageBook.service.BookService" %>
<%@ page import="com.ld.manageBook.service.impl.BookServiceImpl" %>
<%@ page import="com.ld.manageBook.service.BorrowStatusService" %>
<%@ page import="com.ld.manageBook.service.impl.BorrowStatusImpl" %>
<%@ page import="com.ld.manageBook.bean.Borrow" %>
<%@ page import="java.sql.Date" %>
<%@ page import="java.time.LocalDate" %>
<%@ page import="com.ld.manageBook.bean.Books" %>
<%@ page import="com.ld.manageBook.bean.BorrowStatus" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-02
  Time: 21:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>还书页面</title>
</head>
<body>
<%!
    private ReturnService returnService = new ReturnServiceImpl();
	BookService bookService = new BookServiceImpl();
	BorrowService borrowService = new BorrowImpl();
	BorrowStatusService borrowStatusService = new BorrowStatusImpl();
%>
<%
    User user = (User)(session.getAttribute("user"));
	Integer returnNumber = Integer.parseInt(request.getParameter("returnNumber"));
    Borrow borrow =(Borrow)(session.getAttribute("currentBorrow"));
	Books book = bookService.getBookById(borrow.getBook_id());
	int needReturnNumber = borrowService.getNeedReturnById(borrow.getId());
	if(needReturnNumber == 0){
		out.println("不需要还此书.");
	}else if(returnNumber > needReturnNumber) {
		out.println("还书数量超过上限");
    }else if(returnNumber <= 0){
		out.println("请输入正确的还书数量");
    }else {
		returnService.returnBook(user.getUsername(),borrow.getBook_id(), Date.valueOf(LocalDate.now()),returnNumber);
		borrowService.updateNeedReturnById(borrow.getId(),returnNumber);
		bookService.returnBook(book.getName(),returnNumber);
		borrowStatusService.returnBorrowBook(user.getUsername(),returnNumber);
		out.println("还书成功");
    }
	out.println("<a href='/home/application.jsp'>返回用户主页</a>");
%>
</body>
</html>
